<template>
	<uv-popup ref="popupRef" mode="right">
		<view class="popup-dia">
			<scroll-view scroll-y class="scroll">
				<view class="card" v-for="(item, index) in allList" :key="index">
					<view class="title">{{ item.title }}</view>
					<view class="btns">
						<button class="cu-btn" :class="{ 'cu-btn-active': item.active.includes(el) }"
							v-for="(el, n) in item.childer" :key="el" @click="handSelect(item, el)">
							{{ el }}
						</button>
					</view>
				</view>
			</scroll-view>
			<view class="action">
				<button class="cu-btn" @click="handReset">重置</button>
				<button class="cu-btn" @click="handSubmit">确定</button>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		reactive,
		ref,
		defineProps,
		defineEmits
	} from 'vue'
	import {
		doLabels
	} from '@/apis/system.js'
	const props = defineProps({
		isOne: {
			type: Boolean,
			default: false
		},
		isFilter: {
			type: Boolean,
			default: false
		},
		isRequire: {
			type: Boolean,
			default: true
		}
	})
	const emit = defineEmits(['change'])
	const allList = ref([])
	const handSelect = (item, el) => {
		if (item.isMore) {
			if (item.active.includes(el)) {
				item.active = item.active.filter(ele => ele !== el)
			} else {
				item.active.push(el)
			}
			if (item.active.includes('全部')) {
				item.childer.forEach(ele => {
					if (!item.active.includes(ele)) {
						item.active.push(ele)
					}
				})
			} else if (el === '全部') {
				item.active = []
			}
		} else {
			item.active = [el]
		}
	}
	const handReset = () => {
		allList.value.map(el => {
			el.active = []
			return el
		})
	}
	const handSubmit = () => {
		if (props.isOne) {
			if (props.isRequire) {
				const info = allList.value.find(el => el.active.length === 0)
				if (info) {
					uni.$uv.toast(`请选择${info.title}`)
					return
				}
			}
			const queryForm = {}
			allList.value.forEach(el => {
				if (el.isMore) {
					queryForm[el.key] = el.active
				} else if (el.active.length > 0) {
					queryForm[el.key] = el.active[0]
				}
			})
			emit('change', queryForm)
		} else {
			const queryForm = {}
			allList.value.forEach(el => {
				if (el.isMore) {
					queryForm[el.key] = el.active
				} else if (el.active.length > 0) {
					queryForm[el.key] = el.active[0]
				} else {
					queryForm[el.key] = ''
				}
			})
			emit('change', queryForm)
		}
		popupRef.value.close()
	}
	const popupRef = ref(null)
	const handOpen = async (row) => {
		const data = await doLabels()
		allList.value = data.map(el => {
			const childer = el.content
			if (!props.isOne) {
				childer.unshift('全部')
			}
			if (props.isFilter) {
				el.isMore = false
			}
			let active = []
			if (row[el.labelKey]) {
				if (el.isMore) {
					active = row[el.labelKey]
				} else {
					active = [row[el.labelKey]]
				}
			}
			return {
				title: el.name,
				key: el.labelKey,
				isMore: el.isMore,
				active,
				childer: el.content
			}
		})
		popupRef.value.open()
	}
	defineExpose({
		handOpen
	})
</script>

<style lang="scss" scoped>
	.popup-dia {
		width: 600rpx;
		height: 100vh;
		padding: 24rpx;
		box-sizing: border-box;
		position: relative;
		display: flex;
		flex-direction: column;

		.scroll {
			height: calc(100vh - 160rpx);
			height: calc(100vh - 160rpx - constant(safe-area-inset-bottom));
			height: calc(100vh - 160rpx - env(safe-area-inset-bottom));
		}

		.card {
			&:not(:first-child) {
				margin-top: 40rpx;
			}

			.title {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.btns {
				display: flex;
				flex-wrap: wrap;
				gap: 20rpx;
				margin-top: 24rpx;

				.cu-btn {
					width: 170rpx;
					height: 68rpx;
					background: #F5F5F5;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					margin: 0;
					border: 1rpx solid #F5F5F5;

					&::after {
						border: none;
					}

					&-active {
						background: #FFF7E4;
						color: #FF930D;
						border-color: #FF930D;
					}
				}
			}
		}

		.action {
			position: absolute;
			width: calc(100% - 48rpx);
			bottom: 20rpx;
			bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
			bottom: calc(env(safe-area-inset-bottom) + 20rpx);
			display: flex;
			align-items: center;
			justify-content: space-between;

			.cu-btn {
				width: 258rpx;
				height: 90rpx;
				background: #F5F5F5;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				font-size: 30rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: center;

				&::after {
					border: none;
				}

				&:last-child {
					background-color: #FFC434;
				}
			}
		}
	}
</style>